<?php
/**
 * Created by PhpStorm.
 * User: 毛文科
 * Date: 2018\10\28 0028
 * Time: 10:03
 */
namespace app\admin\controller;
use think\Db;
use think\Request;
use think\Validate;
use tp5er\Backup;

class Database
{
    /**加载备份页面
     * @return \think\response\View
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function showdata(){
        $data = Db::name('database_sql')->select();
        $type = Db::name('database_type')->where('id',1)->find();
        return view('showdata',['backups'=>$data,'type'=>$type]);
    }

    /**修改备份的方式
     * @return int|string
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function showzbf(Request $request){
        $set = $request->param('data',null);
        if(!$set){
            return '请填入数据库备份的类型';
        }
        if(Db::name('database_type')->where('id',1)->update(['type'=>$set])){
            return 1;
        }else{
            return '修改失败';
        }
    }

    /**加载编辑备份设置
     * @return \think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function edit(){
        $data = Db::name('database_type')->find();
        return view('edit',['data'=>$data]);
    }

    /**更新备份配置
     * @return int|string
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function update(Request $request){
        $data = $request->post();
        $val = new Validate([
            'id'=>'require'
        ]);
        if($data['time']<1){
            return '请输入正确的保存时间';
        }
        $data['update_time'] = time();
        if(Db::name('database_type')->where('id',$data['id'])->update($data)){
            return 1;
        }else{
            return '修改失败';
        }
    }
    /**数据库手动备份接口
     * @return string
     */
    public function beifen(Request $request){
        $data['remarks'] = $request->param('beizhu',null);
        $db = new Backup();
//        return 'haha';
        $files = $db->dataList();
//        return $files;
        $name = date('Y-m-d',time()).'-'.time();
        foreach($files as $value){
            if($value['name']!=config('prefix').'database_sql')
            $db->setFile(['name'=>$name,'part'=>1])->backup($value['name'],0);
        }
        $data['remarks'] = $request->param('beizhu','自动备份');
        $name = $name.'-1.sql';
        $type = Db::name('database_type')->find();
        $data['create_time'] = \time();
        $data['type'] = $type['type'];
        $data['filename'] = $name;
        $data['url'] = './Data/'.$name;
        if(Db::name('database_sql')->insert($data)){
            return 1;
        }else{
            return '备份失败';
        }
    }

    /**数据库文件下载
     * @param $id
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function xiazai($id){
        if(!$id){
            return '请填写下载的id';
        }
        $data = Db::name('database_sql')->where('id',$id)->find();
        $name = $data['filename'];
        $file=fopen($data['url'],"r");
        header("Content-Type: application/octet-stream");
        header("Accept-Ranges: bytes");
        header("Accept-Length: ".filesize($data['url']));
        header("Content-Disposition: attachment; filename=".$name);
        echo fread($file,filesize($data['url']));
        fclose($file);
    }

    /**删除备份表
     * @return string
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function del(Request $request){
        $id = $request->param('id',null);
        if(!$id){
            return '请输入删除的id';
        }
        $data = Db::name('database_sql')->where('id',$id)->find();
//        return unlink($data['url']);
        if(Db::name('database_sql')->where('id',$id)->delete()){
            if(is_file($data['url'])){
                unlink($data['url']);
            }
            return 1;
        }else{
            return '删除失败';
        }
    }

    /**数据库还原接口
     * @return int|string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function huanyuan(Request $request){
        $id = $request->param('id',null);
        if(!$id){
            return '请输入还原的数据表id';
        }
        $db = new Backup();
        $data = Db::name('database_sql')->where('id',$id)->find();
        $db->setFile(['name'=>$data['url'],'part'=>1])->import(0);
        if(is_file($data['url'])){
            unlink($data['url']);
        }
        return 1;
    }

    /**数据库自动备份接口
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function automatic(Request $request){
      //return 1;
        $type = Db::name('database_type')->find();
        $database_sql = Db::name('database_sql')->order('create_time desc')->select();
      //return $type['time'];
        if($type['type']==1){
            if($database_sql){
              //return $type['time']*3600*24;
                foreach ($database_sql as $value){
                  //return var_dump(time()>=($value['create_time']+$type['time']*3600*24));
                    if(time()>=($value['create_time']+$type['time']*3600*24)){
                      if(time()>=($value['create_time']+$type['time']*3600*24)){
                        if(is_file($value['url'])){
                            unlink($value['url']);
                        }
                          Db::name('database_sql')->where('id',$value['id'])->delete();
                    }
                    }
                }
                if(time()>($database_sql[0]['create_time']+$type['jian_time']*3600*24)){
                    return $this->beifen($request);
                }else{
                    return 0;
                }
            }else{
                return $this->beifen($request);
            }
        }else{
            return 0;
        }

    }
}